﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Handrise.CDCable.Mobile.Business;
using HandRise.MBHOP.Controls;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmTask :FrmBase
     {
        private PlanService planService;
        private List<Plan> plans;
        private ImageList ilstup = new ImageList();
        private ImageList ilstdown = new ImageList();
        private Synchronize synchronize;
        private bool isDownloading = false;

        public FrmTask()
        {
            InitializeComponent();
            ttlbrhead.Text = "巡视管理";
            ilstup.ImageSize= new Size(80, 41);
            ilstdown.ImageSize=new Size(80,41);
            ilstup.Images.Add(ResourceImage.过滤01);
            ilstup.Images.Add(ResourceImage.下载01);
            ilstup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.过滤02);
            ilstdown.Images.Add(ResourceImage.下载02);
            ilstdown.Images.Add(ResourceImage.返回02);
            bctask.ItemDownImage = ilstdown;
            bctask.ItemUpImage = ilstup;
            ilPlanState.Images.Add(ResourceImage.旗子绿);
            ilPlanState.Images.Add(ResourceImage.旗子蓝色);
            ilPlanState.Images.Add(ResourceImage.旗子灰);
        }

        private void Fxunjian_Load(object sender, EventArgs e)
        {
            planService = new PlanService();

            synchronize = new Synchronize();
            synchronize.OnDownloadPlanComplete += new CallWebServiceCompleteHandler(synchronize_OnDownloadPlanComplete);

            cbPlanState.SelectedIndex = 0;
            cbPlanState.SelectedIndexChanged += new EventHandler(cbPlanState_SelectedIndexChanged);

            //如果是返回进入，则加载缓存的数据
            if (IsReturn)  
                LoadCacheData();
            else
                LoadData();

        }

        private void synchronize_OnDownloadPlanComplete(string info, bool isSuccess, object userData)
        {
            pbUpload.Visible = false ;
            pbSynchronize.Visible = false ;
            //bctask.Enabled = true;
            dlsttask.Enabled = true;
            timerShowProcess.Enabled = false ;
            isDownloading = false;
            LoadData();
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        public void LoadCacheData()
        {
            int oldIndex = (int)Caches[0] ;
            if (cbPlanState.SelectedIndex != oldIndex)
                cbPlanState.SelectedIndex = oldIndex;
            else
                cbPlanState_SelectedIndexChanged(null, null);
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            //判断是否是通过设备查询返回的任务列表
            if (Parameter.Count == 1)
            {
                //是通过设备查询进入
                plans = (List<Plan>)Parameter[0];
            }
            else
            {
                //从主窗体进入
                plans = planService.GetDefaultPlans(AppLogic.LoginUser.UserName);
            }


            //绑定数据
            dlsttask.DataSource = plans;
            dlsttask.DataBind();
        }

        private void cbPlanState_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbPlanState.SelectedIndex)
            {
                case 1:
                    plans = planService.GetAllPlans(AppLogic.LoginUser.UserName);
                    break;
                default:
                    plans = planService.GetDefaultPlans(AppLogic.LoginUser.UserName);
                    break;
            }

            //绑定数据
            dlsttask.DataSource = plans;
            dlsttask.DataBind();

            SetButtonState();
        }

        private void SetButtonState()
        {
            btnNext.Enabled = dlsttask.PageCount > 0 && dlsttask.PageIndex < dlsttask.PageCount;
            btnPrior.Enabled = dlsttask.PageCount > 0 && dlsttask.PageIndex > 1;
        }

        private void bcxunjian_OnButtonClick(object sender, int buttonIndex)
        {
            if (isDownloading)
                return;

            switch (buttonIndex)
            {
                case 0:
                    ShowTaskFilter();
                    break;
                case 1: //下载
                    if (AppLogic.netService.CurrentNetType != NetType.None)
                    {
                        pbUpload.Visible = true;
                        pbUpload.Image = ResourceImage.数据同步;
                        pbSynchronize.Visible = true;
                        //bctask.Enabled = false;
                        dlsttask.Enabled = false;
                        timerShowProcess.Enabled = true;
                        isDownloading = true;
                        synchronize.DownloadPlan(AppLogic.LoginUser.UserName);
                    }
                    else
                    {
                        mssdlgexec.ShowDialog(this, "请先联通网络再进行下载", DialogType.Confirm, FormManage.form_Deactivate);
                    }
                    break;
                case 2:
                    FormManage.GotoNext("FrmMain", null, null, true);
                    break;

            }
        }

        /// <summary>
        /// 显示设备过滤窗口
        /// </summary>
        private void ShowTaskFilter()
        {
            FormManage.GotoNext("FrmTaskFilter",new object[]{"FrmTask"},new object[]{cbPlanState.SelectedIndex}, false);
        }

        private void dataList1_OnItemClick(HandRise.MBHOP.Controls.DataListItem item)
        {
            Plan plan = (Plan)item.DataSource;
            FormManage.GotoNext("FrmTaskDetail", new object[] { plan }, new object[] { cbPlanState.SelectedIndex }, false);

        }

        private void btnPrior_Click(object sender, EventArgs e)
        {
            dlsttask.PreviousPage();
            SetButtonState();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            dlsttask.NextPage();
            SetButtonState();
        }

        private void dlsttask_OnItemCreateBefore(HandRise.MBHOP.Controls.DataListItem item)
        {
            Plan plan = (Plan)item.DataSource;

            switch (plan.State)
            {
                case "执行中":
                    item.IconIndex = 1;
                    break;
                case "已完成":
                    item.IconIndex = 2;
                    break;
                default:
                    item.IconIndex = 0;
                    break;
            }

            if (plan.EndDateTime.HasValue && plan.EndDateTime < DateTime.Now)
            {
                item.ForeColor = Color.Red;
            }

        }

        private void timerShowProcess_Tick(object sender, EventArgs e)
        {
            if (pbSynchronize.Value == pbSynchronize.Maximum)
            {
                pbSynchronize.Value = 0;
            }
            else
            {
                pbSynchronize.Value++;
            }
        }



    }
}